<template>
  <view class="c">
    <!-- search-bar -->
    <u-sticky>
      <view
        style="
          height: 76px;
          display: flex;
          flex-direction: column;
          padding: 3px 6px;
          background-color: #fff;
        "
      >
        <li-search
          placeholder="搜索"
          v-model="m_keyword"
          :height="36"
          v-on:search="onSearch"
        />
        <view
          class="p-c"
          style="
            padding: 10px 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <view
            >当前积分
            <text
              style="
                color: #ec3e40;
                font-size: 16px;
                font-weight: bold;
                margin-left: 8px;
              "
              >{{ memberInfo[0].Ponit }}</text
            >
          </view>
          <view>可兑换</view>
        </view>
      </view>
    </u-sticky>

    <!-- goods  -->
    <scroll-view
      v-if="m_goodsList.length > 0"
      class="product-section"
      scroll-y
      scroll-with-animation
      @scrolltolower="onscrolltolower"
    >
      <view class="wrapper">
        <view class="products">
          <view
            class="product"
            v-for="(good, key) in m_goodsList"
            :key="key"
            @tap="goToDetail(good)"
          >
            <!-- imageUrl + -->
            <image :src="good.FProImage" mode="aspectFit" class="image" />
            <view class="content">
              <view class="name">{{ good.FName }}</view>
              <view class="price">
                <view>
                  <text class="symbol">{{ good.FMallPoint }}</text>
                  <text>积分</text>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    <view
      v-else
      style="
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding-top: 50px;
      "
    >
      <image
        src="/static/images/sswk.png"
        style="width: 300rpx"
        mode="widthFix"
      />
      <view style="color: #141414; margin-top: 20px"
        >抱歉，没有找到相关商品</view
      >
      <view style="color: #b39385; margin-top: 10px">换个词试试吧</view>
    </view>

    <li-tab-bar :curIdx="3" />
  </view>
</template>

<script>
import { onMounted, ref } from "@vue/composition-api";
import { reqPost } from "../../common/http.request";
import { storeToRefs } from "pinia";
import { useUser } from "../../store/useUser";
import { useShop } from "../../store/useShop";
import { uGetStorageSync, uNavigateTo, uToast } from "../../utils/uniUtils";

export default {
  components: {},
  onLoad({ kw }) {
    if (kw) {
      this.m_keyword = kw;
    }
  },
  onReachBottom() {
    console.log("onReachBottom");
    if (this.m_goodsList.length < this.m_count) {
      this.m_page_index += 1;
      this.GetPageData();
    }
  },
  setup() {
    onMounted(async function () {
      await onSearch();
    });

    const m_keyword = ref("");
    const m_goodsList = ref([]);
    const hasLogin = storeToRefs(useUser()).hasLogin;
    const cart = storeToRefs(useShop()).cart;
    const tCount = ref(useShop().tCount);
    const tMoney = ref(useShop().tMoney);
    const m_page_index = ref(1);
    const m_count = ref(0);
    const memberInfo = ref(uGetStorageSync("Members"));

    async function onSearch() {
      /*      if (m_keyword.value.length == 0) {
        uToast("请输入关键字~", "error");
        return;
      } */
      m_goodsList.value = [];
      m_page_index.value = 1;
      GetPageData();
    }

    async function GetPageData() {
      const { Data, Count } = await reqPost("GetGoodsList", {
        condition: m_keyword.value,
        class_id: "",
        page_index: m_page_index.value,
        page_size: 10,
        order_type: 0,
        brand_id: 0,
      });
      m_count.value = Count;
      m_goodsList.value = [...m_goodsList.value, ...Data];
    }

    function updateCart() {
      cart.value = useShop().cart;
      tCount.value = useShop().tCount;
      tMoney.value = useShop().tMoney;
    }

    async function handleAddToCart(product) {
      if (useUser().hasLogin) {
        await useShop().AddToCart(product.FItemID, product.FMallPrice);
        cart.value = useShop().cart;
        tCount.value = useShop().tCount;
        tMoney.value = useShop().tMoney;
      } else {
        uNavigateTo("/pages/login/login");
        /*   if (uGetStorageSync("hasBindStore")) {
        } else {
          uToast("未绑定店铺~", "error");
        } */
      }
    }

    async function handleMinusFromCart(product) {
      if (useUser().hasLogin) {
        await useShop().MinusToCart(product.FItemID);
        cart.value = useShop().cart;
        tCount.value = useShop().tCount;
        tMoney.value = useShop().tMoney;
      } else {
        if (uGetStorageSync("hasBindStore")) {
          uNavigateTo("/pages/login/login");
        } else {
          uToast("未绑定店铺~", "error");
        }
      }
    }

    function goToDetail(goods) {
      uNavigateTo(
        "/pages/goods/detail?id=" + goods.FItemID + "&point=" + goods.FMallPoint
      );
    }

    function onscrolltolower(e) {
      console.log("onscrolltolower");
    }

    return {
      m_keyword,
      m_goodsList,
      hasLogin,
      cart,
      tCount,
      tMoney,
      updateCart,
      onSearch,
      handleAddToCart,
      handleMinusFromCart,
      goToDetail,
      onscrolltolower,
      m_page_index,
      m_count,
      GetPageData,
      memberInfo,
    };
  },
};
</script>

<style lang="scss">
.product-section {
  margin-top: 18px;
  flex: 1;
  height: calc(100vh-46px);
  .wrapper {
    padding: 0 20rpx;
    padding-bottom: 130rpx;
  }

  .ads1,
  .ads2 {
    height: 300rpx;
  }
  .ads1 {
    margin-bottom: 20rpx;
  }

  .category-name {
    padding: 30rpx 0;
    font-size: 26rpx;
    color: #777;
  }

  .products {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    flex-wrap: wrap;
    margin-bottom: 20rpx;
  }

  .product {
    width: 47.5%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 10px;
    border-radius: 8px;
    position: relative;
    background-color: #fff;
    padding: 5px 0;
    .mask {
      width: 540rpx;
      height: 200rpx;
      background: rgba(17, 17, 17, 0.8);
      position: absolute;
      top: 0;
      left: 0;
      z-index: 9;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .image {
      width: 300rpx;
      height: 300rpx;
      margin-right: 10rpx;
      flex-shrink: 0;
    }

    .content {
      /*      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 200rpx;
      flex: 1; */

      padding-left: 20px;
      .name {
        font-size: 14px;
        width: 360rpx;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/
        -webkit-box-orient: vertical; /** 设置或检索伸缩盒对象的子元素的排列方式 **/
        -webkit-line-clamp: 2; /** 显示的行数 **/
        overflow: hidden; /** 隐藏超出的内容 **/
      }

      .labels {
        display: flex;
        font-size: 20rpx;
        margin-bottom: 8rpx;
        overflow: hidden;
        flex-wrap: wrap;
        .label {
          max-width: 40%;
          padding: 6rpx 10rpx;
          margin-right: 10rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }

      .description {
        margin-bottom: 20rpx;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        color: #777;
        font-size: 24rpx;
      }

      .price {
        color: #777;
        font-size: 12px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .symbol {
          font-size: 18px;
          font-weight: bold;
          color: #ec3e40;
        }
      }
    }
  }
}
</style>
